package com.campsys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campsys.common.QueryPageParam;
import com.campsys.common.Result;
import com.campsys.entity.Activity;
import com.campsys.entity.Teacher;
import com.campsys.entity.User;
import com.campsys.service.TeacherService;
import com.campsys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin(origins = "http://localhost:8080")
@RequestMapping({"/teacher"})
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @PostMapping("/list")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        Page<Teacher> page =new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Teacher> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String message = (String) param.get("message");
        if (StringUtils.isNotBlank(message)){
            lambdaQueryWrapper.like(Teacher::getTeacherName,message)
                    .or()
                    .like(Teacher::getSubject,message);
        }
        IPage result =teacherService.page(page,lambdaQueryWrapper);
        return Result.suc(result.getTotal(),result.getRecords());
    }
    @PostMapping("/save")
    public Result createTeacher(@RequestBody Teacher teacher){
        return teacherService.createTeacher(teacher)?Result.suc():Result.fail();
    }
    @PostMapping("/update")
    public Result update(@RequestBody Teacher teacher){
        return teacherService.updateById(teacher)?Result.suc():Result.fail();
    }
    @GetMapping("/del")
    public Result del(@RequestParam Integer id){
        return teacherService.removeById(id)?Result.suc():Result.fail();
    }
    @GetMapping("/getTeachers")
    public List<Teacher> getTeacher(){
        return teacherService.getTeachers();
    }
}
